package net.maku.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.member.convert.MemberTagConvert;
import net.maku.member.entity.MemberTagEntity;
import net.maku.member.query.MemberTagQuery;
import net.maku.member.vo.MemberTagVO;
import net.maku.member.dao.MemberTagDao;
import net.maku.member.service.MemberTagService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员标签
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-16
 */
@Service
@AllArgsConstructor
public class MemberTagServiceImpl extends BaseServiceImpl<MemberTagDao, MemberTagEntity> implements MemberTagService {

    @Override
    public PageResult<MemberTagVO> page(MemberTagQuery query) {
        IPage<MemberTagEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(MemberTagConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    @Override
    public List<MemberTagVO> getList() {
        return BeanUtil.copyToList(this.list(), MemberTagVO.class);
    }

    private LambdaQueryWrapper<MemberTagEntity> getWrapper(MemberTagQuery query) {
        LambdaQueryWrapper<MemberTagEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(MemberTagVO vo) {
        MemberTagEntity entity = MemberTagConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(MemberTagVO vo) {
        MemberTagEntity entity = MemberTagConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}